<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/18
 * Time: 12:57
 */

namespace app\admin\model;

use think\Model;
use think\Db;

class StockModel extends Model
{
    //库存列表
    public function stockList()
    {
        $data = Db::table('ajw_main_stock')
            ->alias('a')
            ->join('ajw_goods g', 'a.goods_id = g.goods_id')
            ->field('a.batch_id ,g.name ,a.jinjia ,a.iprice,a.iiprice,a.iiiprice,a.ivprice,a.number,a.status')
            ->paginate(10);
        return $data;
    }

    //查询出商品的详情
    public function goodsContent($goods_id)
    {

        $data = Db::table('ajw_main_stock')
            ->alias('a')
            ->join('ajw_goods g', 'a.goods_id = g.goods_id', 'RIGHT')
            ->field('a.batch_id ,g.name ,a.jinjia ,a.iprice,a.iiprice,a.iiiprice,a.ivprice,a.number,a.status ,g.goods_id')
            ->where('g.goods_id', $goods_id)
            ->where('a.status', 1)
            ->find();
        if (!$data) {
            $data = Db::table('ajw_main_stock')
                ->alias('a')
                ->join('ajw_goods g', 'a.goods_id = g.goods_id', 'RIGHT')
                ->field('a.batch_id ,g.name ,a.jinjia ,a.iprice,a.iiprice,a.iiiprice,a.ivprice,a.number,a.status ,g.goods_id')
                ->where('g.goods_id', $goods_id)
                ->find();
        }
        return $data;
    }

    //写入库存
    public function writekuchun($data)
    {
        $res = Db::table('ajw_main_stock')
            ->insertAll($data);
        return $res;
    }

    //退库列表查询
    public function outStockList ($goods_id)
    {
        $data = Db::table('ajw_main_stock')
            ->alias('a')
            ->join('ajw_goods g', 'a.goods_id = g.goods_id')
            ->field('a.batch_id ,g.name ,a.jinjia ,a.iprice,a.iiprice,a.iiiprice,a.ivprice,a.number,a.status ,g.goods_id')
            ->where('g.goods_id', $goods_id)
            ->where('a.status','<>',-1)
            ->paginate(10);
        return $data;
    }
    //退库操作
    public function outStock ($param,$goods_id)
    {
        $outshuliang = $param['outshuliang'];
        $outbatch_id = $param['outbatch_id'];
        $yuanyin = $param['outyuanyin'];
        $res = Db::table('ajw_main_stock')
            ->where('batch_id', $outbatch_id)
            ->setDec('number', $outshuliang);
            $res1 = true;
        if ($res == true) {
            $number = Db::table('ajw_main_stock')
                ->where('batch_id', $outbatch_id)
                ->value('number');

            if ($number < 0 || $number == 0) {
                $data['number'] = 0;
                $data['status'] = -1;
                $res1 = DB::table('ajw_main_stock')
                    ->where('batch_id', $outbatch_id)
                    ->update($data);
            }
        }
        //添加到退库列表中
            $time = time();
            $outstockData = [
            'batch_id' => $outbatch_id,
            'goods_id' => $goods_id,
            'number' => $outshuliang,
            'create_time' =>$time,
            'yuanyin' =>$yuanyin

        ];
        $res2 = Db::table("ajw_out_stock")
        ->insert($outstockData);
       return ( $res && $res1 && $res2);
    }

    //查询退库列表
    public function outStockList2 ()
    {
        $data = Db::table('ajw_out_stock')
            ->alias('a')
            ->join('ajw_goods g','a.goods_id = g.goods_id')
            ->paginate(10);
        return $data;
    }
}
